<?php
class AdminModel extends Model {
	
	//自动验证
	protected $_validate = array(
       array('account','require','账号必须'),
       array('password','require','密码必须',0,'',1),
       array('nickname','require','昵称必须'),
       array('rpassword','password','两次密码不一致',0,'confirm'), 
       array('account','','帐号已经存在',0,'unique',1),
       array('email','email','邮箱格式错误'),
       array('account','checkAccount','账号己存在',0,'callback',2),
       );
     //自动完成  
    public $_auto		=	array(
        array('password','md5',1,'function'),
        array('create_time','time',1,'function'),
        array('update_time','time',2,'function'),
        array('password','checkPwd',2,'callback'),
        );

  

//取得管理员用户列表
   public function getAdminList($page,$filter="")
   {
   	//echo $filter['username'];
   	 if(!empty($filter['username']))
   	 {
   	 	 $where['account']=array("like","%admin%");
   	 	}

   	  //$where[1]=1;
   	  $admin_list= $this->field('pe_admin.id as aid,account,nickname,email,pe_admin.status,pe_role.name as rname,pe_admin.create_time as c_time')
   	                    ->join('pe_role ON pe_admin.type_id=pe_role.id')
   	                    ->where($where)
   	                    ->limit($page->getLimit())
   	                    ->select();
   	  return $admin_list;
   }

 //取得当前管理员的资料
 
  public function getAdminInfo($id)
  {
  	$where['pe_admin.id']=$id;
  	
  	$admin_info=$this->field('pe_admin.id as aid,account,password,nickname,email,pe_admin.status,pe_role.name as rname,pe_admin.create_time as c_time,pe_role.id as rid')
   	                    ->join('pe_role ON pe_admin.type_id=pe_role.id')
   	                    ->where($where)
   	                    ->find();
   	return $admin_info;
  }

//得到角色的信息

 public function getRoleInfo()
 {
 	 $role=M('Role');
 	 
 	 $role_info=$role->where('status=1')->select();
 	 return $role_info;
 }
 
 //验证编辑时账号是否存在
 public function checkAccount()
 {
 		if($_POST['account']==$_POST['orgaccount'])
 		{
 		   return true;	
 		}
 		else
 		{
 			$where['account']=$_POST['account'];
 			if($this->where($where)->find())
 			{
 				 return false;
 			}
 			else
 			{
 				 return true;
 			}
 		}
 }
 
 //编辑时验证密码的情况，当不填时就是原密码
 
 public function checkPwd()
 {
 	  if(empty($_POST['password']))
 	  {
 	  	$admin_info= $this->getAdminInfo($_GET['id']);
 	  	return $admin_info['password'];
 	  }
 	 else
 	  {
 	  	return md5($_POST['password']);
 	  }
 }
}

?>